package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

// https://schoolido.lu/api/cards/?page_size=100&page=1
// getSifCardInfoByPage.js
func main() {

	fmt.Println("请输入sif文件夹地址：")

	scanner := bufio.NewReader(os.Stdin)
	pathName, _, _ := scanner.ReadLine()

	//var pathName string = "F:\\IDM下载\\新建文件夹"

	nameFile, _ := os.Open(string(pathName) + "/name.txt")

	reader := bufio.NewReader(nameFile)
	nameMap := make(map[string]string)
	for true {
		line, _, _ := reader.ReadLine()
		if len(line) == 0 {
			break
		}
		nameIndex := bytes.IndexByte(line, '~')
		nameMap[string(line[0:nameIndex])] = strings.NewReplacer("/", "╱", "?", "？").Replace(string(line[nameIndex+1:]))
	}

	cardPath := string(pathName) + "/card/"
	cardDir, _ := ioutil.ReadDir(cardPath)
	for _, card := range cardDir {
		cardId0 := card.Name()
		i := strings.LastIndex(cardId0, ".")
		ex := cardId0[i:]

		var cardName string
		if cardId0[0:4] == "card" {
			if cardId0[i-1] == 't' {
				cardId := cardId0[strings.Index(cardId0, "_")+1 : strings.LastIndex(cardId0, "_")]
				id := "000" + cardId
				cardName = "SIF卡牌-" + id[len(id)-4:] + "-" + strings.Replace(nameMap[cardId], "_idolized_", "-觉醒-", 1) + ex
			} else {
				cardId := cardId0[strings.Index(cardId0, "_")+1 : strings.LastIndex(cardId0, "_")]
				id := "000" + cardId
				cardName = "SIF卡牌-" + id[len(id)-4:] + "-" + strings.Replace(nameMap[cardId], "_idolized_", "-普通-", 1) + ex

			}
		} else {
			if cardId0[i-1] == 't' {
				cardId := cardId0[strings.Index(cardId0, "_")+1 : strings.LastIndex(cardId0, "_")]
				id := "000" + cardId
				cardName = "SIF立绘-" + id[len(id)-4:] + "-" + strings.Replace(nameMap[cardId], "_idolized_", "-觉醒-", 1) + ex
			} else {
				cardId := cardId0[strings.Index(cardId0, "_")+1 : i]
				id := "000" + cardId
				cardName = "SIF立绘-" + id[len(id)-4:] + "-" + strings.Replace(nameMap[cardId], "_idolized_", "-普通-", 1) + ex
			}
		}

		_ = os.Rename(cardPath+cardId0, cardPath+cardName)
		fmt.Println(cardId0 + " 被重命名为 " + cardName)
	}
	fmt.Println("end! ")

}
